package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.data.DataResult;
import com.dongdong.data.ResponseStatusEnum;
import com.dongdong.entity.OwnPageInfo;
import com.dongdong.pojo.TbGoods;
import com.dongdong.pojo.TbSeller;
import com.dongdong.service.GoodsService;
import com.dongdong.service.SellerService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/13 13:05
 * @Version 1.0
 **/
@Controller
@RequestMapping("sellerController")
public class SellerController {

    @Reference
    private SellerService sellerService;

    @Reference
    private GoodsService goodsService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    private static final Logger logger = LoggerFactory.getLogger(SellerController.class);

    @RequestMapping("list")
    public String list(){
        return "admin/seller";
    }

    @RequestMapping("getSeller")
    @ResponseBody
    public DataResult getSeller(TbSeller tbSeller){
        try {
            List<TbSeller> tbSellerList = sellerService.getSeller(tbSeller);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbSellerList);
        }catch (Exception e){
            logger.error("查询失败",e);
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getMessage")
    @ResponseBody
    public DataResult getMessage(String sellerId){
        try {
            TbSeller tbSellers = sellerService.getMessage(sellerId);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbSellers);
        }catch (Exception e){
            logger.error("查询详情失败",e);
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateStatus")
    @ResponseBody
    public DataResult updateStatus(String sellerId,String status){
        try {
            TbSeller tbSeller = new TbSeller();
            tbSeller.setSellerId(sellerId);
            tbSeller.setStatus(status);
            int status1 = sellerService.updateStatus(tbSeller);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(status1);
        }catch (Exception e){
            logger.error("状态修改失败",e);
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("tolist")
    public String tolist(){
        return "admin/goods";
    }


    @RequestMapping("getSellerCheck")
    @ResponseBody
    public DataResult getSellerCheck(String goodsName,@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "10") Integer pageSize){
        try {
            OwnPageInfo<TbGoods> tbGoods = goodsService.SellerCheck(goodsName,pageNum,pageSize);
            return DataResult.response(ResponseStatusEnum.SUCCESS).setData(tbGoods);
        }catch (Exception e){
            logger.error("状态修改失败",e);
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateSellerS")
    @ResponseBody
    public DataResult updateSellerS(@RequestParam("ids[]") Long[] ids,String auditStatus){
        System.out.println(auditStatus);
        try {
            int i = goodsService.updateSellerS(ids,auditStatus);
            if (i > 0) {
                rocketMQTemplate.convertAndSend("goods_topic", ids);
                return DataResult.response(ResponseStatusEnum.SUCCESS);
            }else {
                return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
            }
        }catch (Exception e){
            logger.error("状态修改失败",e);
            return DataResult.response(ResponseStatusEnum.INTERNAL_SERVER_ERROR);
        }
    }
}

